#include "StdAfx.h"
#include "FileDescriptorReal.h"
#include "DirectoryTree.h"

namespace ja2{ namespace vfs
{
	class DirectoryTree::IteratorImpl : public IDirectory::Iterator::IImplementation
	{
		// Attributes
	private:
		//! Iterator location.
		FileCatalog_t::const_iterator m_Location;

		// Operations
	public:
		//! See base.
		virtual FileProxy value() const;
		//! See base.
		virtual void increment();
		//! See base.
		virtual bool equal(IImplementation const &Right) const;
		//! See base.
		virtual Ptr_t clone() const;

		// Construction/Destruction
	public:
		IteratorImpl(FileCatalog_t::const_iterator const &Location);
	};

	DirectoryTree::IteratorImpl::IteratorImpl(FileCatalog_t::const_iterator const &Location)
		: m_Location(Location)
	{
	}

	FileProxy DirectoryTree::IteratorImpl::value() const
	{
		return m_Location->second;
	}

	void DirectoryTree::IteratorImpl::increment()
	{
		++m_Location;
	}

	bool DirectoryTree::IteratorImpl::equal(IImplementation const &Right) const
	{
		return (m_Location == static_cast<IteratorImpl const &>(Right).m_Location);
	}

	DirectoryTree::IteratorImpl::Ptr_t DirectoryTree::IteratorImpl::clone() const
	{
		return Ptr_t(new IteratorImpl(m_Location));
	}

	//-------------------------------//

	DirectoryTree::DirectoryTree(boost::filesystem::path const &MountPoint, boost::filesystem::path const &RealPath)
		: DirectoryBase(MountPoint, RealPath)
	{
	}

	FileProxy DirectoryTree::get_file(boost::filesystem::path const &File) const
	{
		assert(file_exist(File));

		return m_FileCatalog.find(File)->second;
	}

	bool DirectoryTree::file_exist(boost::filesystem::path const &File) const
	{
		return (m_FileCatalog.count(File.parent_path()) == 1);
	}
	
	PathVector_t DirectoryTree::get_sub_dirs() const
	{
		return PathVector_t();
	}
	
	DirectoryTree::Iterator DirectoryTree::begin()
	{
		return DirectoryTree::Iterator(new IteratorImpl(m_FileCatalog.begin()));
	}
	
	DirectoryTree::Iterator DirectoryTree::end()
	{
		return DirectoryTree::Iterator(new IteratorImpl(m_FileCatalog.end()));
	}
	
	void DirectoryTree::add_file(FileDescriptorPtr_t const &pFileDesc, bool DeleteOldFile)
	{
		boost::filesystem::path const &file_path = pFileDesc->get_file_name();
		// File exist and we don't want to overwrite
		if(file_exist(file_path) && !DeleteOldFile)
			return;

		m_FileCatalog[file_path] = FileProxy(pFileDesc);
	}

	void DirectoryTree::create_subdirectory(boost::filesystem::path const &SubDir)
	{
	}

	void DirectoryTree::delete_subdirectory(boost::filesystem::path const& Dir)
	{
	}

	void DirectoryTree::delete_file(boost::filesystem::path const& File)
	{
	}
} /*vfs*/} /*ja2*/